<template>
	<view>
		<view class="search-box">
			<uni-search-bar @confirm="search" @input="input" placeholder="请输入搜索内容" :radius="100" cancelButton="none"></uni-search-bar>
		</view>
		<!-- 搜索建议 -->
		<view class="search-list" v-if="searchResult.length !== 0">
			<view class="search-item" v-for="(item,index) in searchResult" :key="index" @click="gotoDetail(item)">
				<view class="title">{{item.goods_name}}</view>
				<uni-icons class="arrow-image" type="arrowright" size="20"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<view class="search-title">
				<text>搜索历史</text>
				<uni-icons class="trashImage" type="trash" @click="deleteHistory"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag :text="kw" v-for="(kw,index) in histories" :key="index" @click="gotoGoodslist(kw)"></uni-tag>
				<!-- <view class="history-item" v-for="(item,index) in searchHistory" :key="index">{{item}}</view> -->
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				keyWord: '',//搜索关键词
				searchResult: [],//搜索结果
				searchHistory: []//历史搜索记录
			};
		},
		onLoad() {
			this.searchHistory = [...this.searchHistory,...JSON.parse(uni.getStorageSync('kw') || '[]')] 
			console.log('111111',this.searchHistory)
		},
		methods:{
			//处理input事件
			input(e){
				clearTimeout(this.timer)
				this.timer = setTimeout(()=>{
					console.log(e)
					this.keyWord = e
					this.getSearchResult()
					//计入搜索历史数据中
					this.saveHistory()
				},500)
			},
			async getSearchResult(){
				if(this.keyWord.length === 0) {
					this.searchResult = []
					return
				}
				const {data:res} = await uni.$http.get('/goods/qsearch?query='+this.keyWord)
				if(res.meta.status !== 200) {
					uni.$showMsg()
				}
				this.searchResult = res.message
				if(this.searchResult.length === 0){
					uni.showToast({
						title:'sorry暂无数据'
					})
				}
			},
			gotoDetail(item){
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
				})
			},
			saveHistory(){
				// this.searchHistory.push(this.keyWord)
				const set = new Set(this.searchHistory)
				set.delete(this.keyWord)
				set.add(this.keyWord)
				this.searchHistory = Array.from(set)
				console.log(set)
				// 对历史搜索数据持久化存储
				uni.setStorageSync('kw', JSON.stringify(this.searchHistory))
				console.log('持久化存储',JSON.stringify(this.searchHistory))
			},
			// 清空历史所搜记录
			deleteHistory() {
				console.log('移除了')
				this.searchHistory = []
				uni.setStorageSync('kw','[]')
				// uni.clearStorageSync('kw')
			},
			gotoGoodslist(kw){
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query='+kw
				})
			}
		},
		computed: {
			histories(){
				console.log('计算属性')
				return [...this.searchHistory].reverse()
			}
		}
	}
</script>

<style lang="scss">
	.search-box{
		position: sticky;
		top: 0;
		z-index: 999;
	}
	.history-box{
		padding: 0 10px;
		.search-title{
			font-size: 15px;
			display: flex;
			justify-content: space-between;
			height: 40px;
			align-items: center;
			border-bottom: 1px solid #efefef;	
		}
		.history-list{
			display: flex;
			flex-wrap: wrap;
			// justify-content: space-around;
			.uni-tag{
				margin-top: 5px;
				margin-right: 5px;
			}
			// .history-item{
			// 	margin-top: 10px;
			// 	margin-right: 10px;
			// }
		}
	}
	.search-list{
		.search-item{
			// height: 40px;
			padding: 10px;
			border-bottom: 1px solid #efefef;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.title{
				font-size: 15px;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
			.arrow-image{
				margin-right: 5px; 
				margin-left: 5px;
			}
		}
	}
</style>
